package com.cxl.controller;


import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.lang.tree.parser.NodeParser;
import com.cxl.entity.City;
import com.cxl.entity.Unit;
import com.cxl.service.ICityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 崔学磊
 * @since 2022-03-16
 */
@RestController
@RequestMapping("/city")
public class CityController {


    @Autowired
    private ICityService iCityService;

    @GetMapping(value = "/getcitylist")
    public  List<Tree<Integer>> cityList() {
        List<City> cities =  iCityService.cityList();

        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setIdKey("value");
        treeNodeConfig.setNameKey("label");
        treeNodeConfig.setParentIdKey("pid");
        treeNodeConfig.setChildrenKey("children");

        List<Tree<Integer>> build = TreeUtil.build(cities, 1, treeNodeConfig, new NodeParser<City, Integer>() {
            @Override
            public void parse(City city, Tree<Integer> treeNode) {
                treeNode.setId(city.getId());
                treeNode.setName(city.getCityname());
                treeNode.setParentId(city.getPid());
            }
        });
        return build;
    }



}
